package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetSetEmailRandModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.SetUserEmailModel;

public class SetEmailActivity extends BaseActivity implements View.OnClickListener {

    EditText setemail_password,setemail_email,setemail_rand;
    ImageView setemail_fh,iv_see_password_em;
    TextView setemail_hqyzm;
    Button setemail_btn;
    private Dialog mDialog;
    String password,rand,email,userid;
    private int i=60;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0){
                setemail_hqyzm.setText("点击重新获取");
                setemail_hqyzm.setClickable(true);
                i=60;
            }
            else{
                setemail_hqyzm.setText(msg.arg1+"秒后可重新发送");
                startTime();
                setemail_hqyzm.setClickable(false);
            }
        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_set_email);

        setemail_password = (EditText)findViewById(R.id.setemail_password);
        setemail_email = (EditText)findViewById(R.id.setemail_email);
        setemail_rand = (EditText)findViewById(R.id.setemail_rand);
        setemail_fh = (ImageView)findViewById(R.id.setemail_fh);
        setemail_hqyzm = (TextView)findViewById(R.id.setemail_hqyzm);
        setemail_btn = (Button)findViewById(R.id.setemail_btn);
        iv_see_password_em = (ImageView)findViewById(R.id.iv_see_password_em);

    }

    @Override
    void initEvent() {
        setemail_hqyzm.setOnClickListener(this);
        setemail_btn.setOnClickListener(this);
        setemail_fh.setOnClickListener(this);
        iv_see_password_em.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.setemail_hqyzm:
                password = setemail_password.getText().toString().trim();
                email = setemail_email.getText().toString().trim();
                userid = getid();

                if (password.equals("")||email.equals("")){
                    Toast.makeText(SetEmailActivity.this,"上面输入信息不能有空",Toast.LENGTH_SHORT).show();
                }
                else if (!isEmail(email)){
                    Toast.makeText(SetEmailActivity.this,"邮箱不合法",Toast.LENGTH_SHORT).show();
                }
                else{
                    mDialog = WeiboDialogUtils.createLoadingDialog(SetEmailActivity.this, "获取中...");
                    GetSetEmailRandModel getSetEmailRandModel = new GetSetEmailRandModel();
                    getSetEmailRandModel.getSetEmailRand(userid,password,email,successBeanTListener);
                }
                break;
            case R.id.setemail_btn:
                password = setemail_password.getText().toString().trim();
                email = setemail_email.getText().toString().trim();
                userid = getid();
                rand = setemail_rand.getText().toString().trim();
                if (password.equals("")||email.equals("")||rand.equals("")){
                    Toast.makeText(SetEmailActivity.this,"所有信息不能为空",Toast.LENGTH_SHORT).show();
                }
                else if (!email.equals(getemail())){
                    Toast.makeText(SetEmailActivity.this,"前后邮箱不一致",Toast.LENGTH_SHORT).show();
                }
                else{
                    SetUserEmailModel setUserEmailModel = new SetUserEmailModel();
                    setUserEmailModel.setUserPhone(userid,password,email,rand,successBeanTListener1);
                }
                break;
            case R.id.setemail_fh:
                finish();
                break;
            case R.id.iv_see_password_em:
                setPasswordVisibility();
                break;

        }

    }
    private void setPasswordVisibility(){
        if (iv_see_password_em.isSelected()) {
            iv_see_password_em.setSelected(false);
            setemail_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            iv_see_password_em.setSelected(true);
            setemail_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }
    private static boolean isEmail(String string) {
        if (string == null)
            return false;
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(string);
        if (m.matches())
            return true;
        else
            return false;
    }

    TListener<SuccessBean> successBeanTListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();
            if (success.equals("1")) {
                Toast.makeText(SetEmailActivity.this, "获取验证码成功", Toast.LENGTH_SHORT).show();
                saveUserInfo();
                startTime();
                DialogThridUtils.closeDialog(mDialog);
            } else if (success.equals("2")) {
                Toast.makeText(SetEmailActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            } else if (success.equals("3")) {
                Toast.makeText(SetEmailActivity.this, "当前邮箱已被注册", Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            } else {
                Toast.makeText(SetEmailActivity.this, "修改错误", Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            }

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(SetEmailActivity.this, "错误", Toast.LENGTH_SHORT).show();
            DialogThridUtils.closeDialog(mDialog);
        }
    };

    TListener<SuccessBean> successBeanTListener1 = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            String success = successBean.getSuccess();
            if (success.equals("1")){
                Toast.makeText(SetEmailActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                finish();
            }
            else if (success.equals("2")){
                Toast.makeText(SetEmailActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
            }
            else if (success.equals("3")){
                Toast.makeText(SetEmailActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(SetEmailActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFail(String msg) {

        }
    };
    private void saveUserInfo() {//存储用户信息
        SharedPreferences.Editor editor = getSharedPreferences("userInfo", MODE_PRIVATE).edit();
        editor.putString("email",email);
        editor.commit();
    }
    public void startTime(){
        Timer timer = new Timer();
        TimerTask task =  new TimerTask() {
            @Override
            public void run() {
                if(i>0){
                    i--;
                    Message message = handler.obtainMessage();
                    message.arg1 = i;
                    handler.sendMessage(message);
                }
            }
        };
        timer.schedule(task,1000);
    }


}
